package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ktjy.ktmall.entity.AddressParams;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.entity.TMenu;
import com.ktjy.ktmall.service.AddressService;
import com.ktjy.ktmall.service.TMenuService;
import com.ktjy.ktmall.service.impl.AddressServiceImpl;
import com.ktjy.ktmall.service.impl.TMenuServiceImpl;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

// 用于处理收货地址相关的请求，比如新增收货地址
@WebServlet(name = "MenuServlet", urlPatterns = "/uni/menu")
public class MenuServlet extends HttpServlet {
    private TMenuService service = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        service = new TMenuServiceImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、创建out对象
        PrintWriter out = response.getWriter();
        String op=request.getParameter("op");
        AjaxResult result = AjaxResult.success("获取菜单总数");

        switch (op){
            case "add":
                TMenu tMenu = new TMenu();
                tMenu.setPsName(request.getParameter("psName"));
                result.setResult(service.addMenu(tMenu));
                break;
            case "edit":
                TMenu tMenu1 = new TMenu();
                tMenu1.setPsName(request.getParameter("psName"));
                tMenu1.setPsId(Integer.parseInt(request.getParameter("psId")));
                result.setResult(service.updateMenu(tMenu1));
                break;
            case "del":
                result.setResult(service.deleteMenu(Integer.parseInt(request.getParameter("psId"))));
                break;
            default:
                result.setResult(service.getMenuList());
                break;
        }





        String rs=JSON.toJSONString(result);
        out.print(rs);

        System.out.println(rs);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
